package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Input_rmb extends AppCompatActivity implements Runnable {
    private static final String TAG = "Rate";
    TextView show;

    private float dollarRate=0.1f;
    private float euroRate=0.05f;
    private float wonRate=500f;
    Handler handler;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_rmb);

        show = findViewById(R.id.rmb_show);
    }

    public void click(View btn) {
        // 获取输入数据
        EditText rmb = findViewById(R.id.rmb);
        String inputStr = rmb.getText().toString();
        try {
            float rmbf = Float.parseFloat(inputStr);
            float result = 0.0f;

            // 进行计算
            if (btn.getId() == R.id.btn_dollar) {
                result = rmbf * dollarRate;
            } else if (btn.getId() == R.id.btn_euro) {
                result = rmbf * euroRate;
            } else if (btn.getId() == R.id.btn_won) {
                result = rmbf * wonRate;
            }

            // 显示输出
            show.setText(String.valueOf(result));
        } catch (NumberFormatException ex) {
            // 消息提示
            Toast.makeText(this, "请输入正确数据", Toast.LENGTH_SHORT).show();
        }
    }

    @SuppressLint("HandlerLeak")
    public void openConfig(View btn) {
        // 打开新的窗口
        Intent config = new Intent(this, Input_rmb2.class);
        // 传递参数
        config.putExtra("dollar_rate_key", dollarRate);
        config.putExtra("euro_rate_key", euroRate);
        config.putExtra("won_rate_key", wonRate);

        Log.i(TAG, "clickOpen: dollarRate=" + dollarRate);
        Log.i(TAG, "clickOpen: euroRate=" + euroRate);
        Log.i(TAG, "clickOpen: wonRate=" + wonRate);

        startActivityForResult(config, 6);

        handler=new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                Log.i(TAG,"handleMessage:收到消息："+msg.what);
                if(msg.what==6){
                    Log.i(TAG,"handleMessage:内容："+msg.obj);
                }
                super.handleMessage(msg);
            }
        };
        //启动线程
        Thread t=new Thread((Runnable) this);
        t.start();
        Log.i(TAG,"onCreate:t.start().....");

    }

    // 处理返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 6 && resultCode == 2) {
            Bundle bundle = data.getExtras();
            dollarRate = bundle.getFloat("dollar_key2", 0f);
            euroRate = bundle.getFloat("euro_key2", 0f);
            wonRate = bundle.getFloat("won_key2", 0f);

            Log.i(TAG, "onActivityResult: dollarRate=" + dollarRate);
            Log.i(TAG, "onActivityResult: euroRate=" + euroRate);
            Log.i(TAG, "onActivityResult: wonRate=" + wonRate);
        }
        super.onActivityResult(requestCode,resultCode,data);




    }


    public void run(){
        Log.i(TAG,"run:run().....");
        try{
            Thread.sleep(30000);
        }catch(InterruptedException e){
            throw new RuntimeException(e);
        }

        //获取网络数据
        URL url=null;
        try {
            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
            Elements tables = doc.getElementsByTag("table");
            Element table2 = tables.get(1);
            //Log.i(TAG,"run:table2="+table2);
            Elements trs = table2.getElementsByTag("tr");
            Elements tds = null;
            for (Element tr : trs) {
                tds = tr.getElementsByTag("td");
            }
            if (tds.size() > 6) {
                String rname = tds.get(0).text();
                String rateStr = tds.get(5).text();
                Log.i(TAG, "run:" + rname + "==>" + rateStr);
                if (rname.equals("欧元")) {
                    Log.i(TAG, "run:欧元汇率=" + rateStr);
                }
                else if (rname.equals("美元")) {
                    Log.i(TAG, "run:美元汇率=" + rateStr);
                }
                else if (rname.equals("韩元")) {
                    Log.i(TAG, "run:美元汇率=" + rateStr);
                }
            }

        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
        //发送数据回主线程
        Message msg=handler.obtainMessage(6);
        msg.obj="Hello Android";
        handler.sendMessage(msg);
        Log.i(TAG,"run:msg已发送");
    }

}
